#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    bool canPartition(vector<int>& nums) {
        int n = nums.size(), m = 0;
        for (auto i : nums) m += i;
        if (m % 2) return false;
        m /= 2;
        vector<int> dp(m + 1, -1);
        dp[0] = 0;
        for (int i = 1; i <= n; i++)
            for (int j = m; j >= nums[i - 1]; j--)
                if (dp[j - nums[i - 1]] != -1) dp[j] = max(dp[j], dp[j - nums[i - 1]] + nums[i - 1]);
        return dp[m] != -1;
    }
};